﻿using System.Collections.Generic;
using System.Linq;
using HappyBa.Core.Data;
using HappyBa.Core.Interfaces;

namespace HappyBa.Core.Validators
{
    public class TaskShouldHaveValidEffort : IValidator
    {
        public IEnumerable<ValidationResult> Validate(ValidationContext context)
        {
            foreach (var entity in context.Metrics.Tasks.Cast<UIEstimatableEntityBase>().Concat(context.Metrics.Bugs).Where(x => x.Effort > 0))
            {
                var assigment = entity.Assignments.FirstOrDefault(x => context.Metrics.IsDeveloper(x.GeneralUser));
                if (assigment != null && !context.ActiveEmployees.Contains(assigment.GeneralUser.ToString())) continue;
                var developer = (assigment == null) ? new User() : assigment.GeneralUser;
                if (entity.Effort > 30)
                {
                    yield return new ValidationResult(
                        string.Format("{0}: {1} ({2}). Too big effort: {3}, developer: {4}", entity.EntityType, entity.Id, entity.EntityState, entity.Effort, developer))
                    {
                        Entity = entity,
                        User = developer
                    };
                }

                if (!context.Metrics.IsFinished(entity))
                {
                    if (entity.TimeSpent >= entity.Effort)
                    {
                        yield return new ValidationResult(
                            string.Format("{0}: {1} ({2}). Spent time {3} is more or equal effort {4}, developer: {5}", entity.EntityType, entity.Id, entity.EntityState, entity.TimeSpent, entity.Effort, developer))
                            {
                                Entity = entity,
                                User = developer
                            };
                    }
                }
                else
                {
                    if (entity.TimeSpent == 0)
                    {
                        yield return new ValidationResult(
                            string.Format("{0}: {1} ({2}). Spent time {3} is zero, developer: {4}", entity.EntityType, entity.Id, entity.EntityState, entity.TimeSpent, developer), ValidationResultType.Warning)
                            {
                                Entity = entity,
                                User = developer
                            };
                        continue;
                    }
                    if (((decimal)1.5 * entity.TimeSpent) < entity.Effort )
                    {
                        yield return new ValidationResult(
                            string.Format("{0}: {1} ({2}). Spent time {3} is much less than effort {4}, developer: {5}", entity.EntityType, entity.Id, entity.EntityState, entity.TimeSpent, entity.Effort, developer), ValidationResultType.Warning)
                            {
                                Entity = entity,
                                User = developer
                            };
                    }
                    if (((decimal)0.5 * entity.TimeSpent) > entity.Effort)
                    {
                        yield return new ValidationResult(
                            string.Format("{0}: {1} ({2}). Spent time {3} is more than effort {4}, developer: {5}", entity.EntityType, entity.Id, entity.EntityState, entity.TimeSpent, entity.Effort, developer), ValidationResultType.Warning)
                            {
                                Entity = entity,
                                User = developer
                            };
                    }
                }

            }
        }
    }
}
